package lanqiao;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

public class D魔法考试 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] nm = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();

        int[] an = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int[] am = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();

        System.out.println(getResult(an,am,nm));
    }

    public static int getResult(int[] an,int[] am,int[] nm) {
        int n = nm[0],m=nm[1];
        int sumnm = n+m;
        int count =0 ;
        HashSet<Integer> set = new HashSet<>();
        for (int i=0;i<n;i++) {
            int s = 0;
            for (int j=0;j<m;j++) {
                s = an[i]+am[j];
                BigInteger num = BigInteger.valueOf(s);
                if (s<=sumnm && num.isProbablePrime(1)) {
                    set.add(s);
                }
            }
        }
        count = set.size();
        return count;
    }
}
